package com.chuanyang.driverh.mainhome.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseListAdapter;
import com.chuanyang.driverh.base.BaseListFragment;
import com.chuanyang.driverh.mainhome.SearchDriverActivity;
import com.chuanyang.driverh.mainhome.adapter.FleetJoinAdapter;
import com.chuanyang.driverh.response2.join.ResultFleetJoinList;
import com.chuanyang.driverh.response2.join.ResultFleetJoinTop;
import com.chuanyang.driverh.responseresult.Result;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.LogUtils;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.StringUtil;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class FleetJoinFragment extends BaseListFragment {
    private FleetJoinAdapter listAdapter;
    private View title_bar;
    private TextView tv_join_num;
    private TextView tv_origin_num;
    private EditText et_search_driver;

    @Override
    public BaseListAdapter createAdapter() {
        return listAdapter = new FleetJoinAdapter(getActivity());
    }

    @Override
    public void initView(View view) {
        super.initView(view);
        title_bar = View.inflate(getContext(), R.layout.home_item_fleetjoin_top, null);
        tv_join_num=(TextView) title_bar.findViewById(R.id.tv_join_num);
        tv_origin_num=(TextView) title_bar.findViewById(R.id.tv_origin_num);
        et_search_driver=(EditText) title_bar.findViewById(R.id.et_search_driver);
        et_search_driver.setOnClickListener(this);
        if (root.getChildCount()>0)root.removeAllViews();
        root.addView(title_bar,new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT,  LinearLayout.LayoutParams.WRAP_CONTENT));
    }

    @Override
    protected void initData() {
        getTopData();
        getData();
    }

    @Override
    protected void initListener() {

    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.et_search_driver:
                Intent intent = new Intent(getActivity(),SearchDriverActivity.class);
                startActivity(intent);
                break;
        }
    }

    private void getTopData() {
        DialogUtil.showLoadingDialog(getActivity(),"加载中...");
        Map<String,Object> params=new HashMap<String, Object>();
        params.put("type", "2");
        params.put("driverId", SharedPrefUtil.getUserId());
        params.put("fleetId", SharedPrefUtil.getFleetId());
        MyHttpUtil.getBusinessToJoin(getActivity(),params, new MyCallback<ResultFleetJoinTop>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
                springView.onFinishFreshAndLoad();
            }
            @Override
            public void onSuccess(ResultFleetJoinTop result, int id) {

                if (result.getCode() == ConstUtil.OK ) {
                    try {
                        tv_join_num.setText(result.getData().getJoinedNum()+"");
                        tv_origin_num.setText(result.getData().getFleetMemberNum()+"");

                    } catch (Exception e) {
                    }
                }else{
                  if (!StringUtil.isBlank(result.getMsg()))
                    PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });
    }



    private void getData() {
        DialogUtil.showLoadingDialog(getActivity(),"加载中...");
        Map<String,Object> params=new HashMap<String, Object>();
        params.put("page", mCurrentPage + "");
        params.put("length", "10");
        params.put("fleetId", SharedPrefUtil.getFleetId());
        params.put("driverId", SharedPrefUtil.getUserId());//ResultFleetJoinList
        MyHttpUtil.selectBusinessByFleet(getActivity(),params, new MyCallback<ResultFleetJoinList>(getActivity(),
                new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
                springView.onFinishFreshAndLoad();
            }
            @Override
            public void onSuccess(ResultFleetJoinList result, int id) {
                if (result.getCode() == ConstUtil.OK ) {
                    try {
                        setDataResult(result.getData().getData());
                    } catch (Exception e) {
                        if (result.getMsg()!=null){
                            PrintUtil.toast(activityContext,result.getMsg());
                        }
                        listAdapter.clear();
                        listAdapter.notifyDataSetChanged();
                    }
                }else{
                    springView.onFinishFreshAndLoad();
                    PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });
    }


    @Override
    public void onGetMore() {
        getData();
    }
    @Override
    public void onRefresh() {
        mCurrentPage = 1;
        getData();
    }

    @Override
    protected void onForceRefresh() {
        onRefresh();
    }

    @Override
    protected boolean isLoadMore() {
        return true;
    }

    @Override
    protected boolean isPullToRefresh() {
        return true;
    }
}
